package CustomClassFile.CronJob;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;

public class TestMSG {
	private final static String randomStr = "task";//定时任务名称拼接的字符串，防止任务名冲突
	private final static int timeM = 5;//上班提醒提前时间  单位分钟
	private final static int timeL = 15;//打卡时间15分钟后再次提醒
	public static void sendMsg() {
		BaseBean writelog = new BaseBean();
		writelog.writeLog("开始添加任务");
		MyQuartzManager.removeJob("mytest1");
		Date date = DateUtil.dateCalculate(new Date(), 1);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("content", "动态定时任务测试");
		List<String> userlist = new ArrayList<String>();
		userlist.add("5");
		userlist.add("3");
		userlist.add("4");
		map.put("userlist", userlist);
		MyQuartzManager.addJob("mytest1", MyJob.class, date, map);

	}

	public static void sendMsgNotice() {
		BaseBean writelog = new BaseBean();
		String content1 = "您好，您上班时间没有打卡，请记得按时打卡！";
		String content2 = "您好，您下班时间没有打卡，请记得按时打卡！  "; 
		
		String pbDate = DateUtil.getCurrentDateTime("yyyy-MM-dd");
		int dateflag=DateUtil.compare_date(DateUtil.getCurrentDateTime("HH:mm:ss"), 
				"23:30:00","HH:mm:ss");
		if (dateflag==1) {//当前时间>23:30,则动态任务的运行时间为下一天开始
			pbDate=DateUtil.datedayCalculateStr(new Date(), 1);
		}
		writelog.writeLog("动态任务执行排班日期为："+pbDate);
		List<Pbzxx> pbzlist = getPBZlist();
		Map<String, List<String>> personMap = getSendMsg(pbDate);
		
		String pbDatepre=DateUtil.datedayCalculateStr(DateUtil.StringToDate(pbDate, "yyyy-MM-dd"), -1);//前一天
		for (Pbzxx pbzxx : pbzlist) {

			writelog.writeLog("开始删除:"+pbzxx.getShift() +"上次的排班组定时任务task");
		
			String jobname1 = pbzxx.getShift() + randomStr + "card1";
			String jobname2 = pbzxx.getShift() + randomStr + "card2";
			String jobname3 = pbzxx.getShift() + randomStr + "card3";
			String jobname4 = pbzxx.getShift() + randomStr + "card4";
			// 移除定时前一天的任务(有可能任务已自动清空)
			MyQuartzManager.removeJob(jobname1+"_1_"+pbDatepre);
			MyQuartzManager.removeJob(jobname2+"_1_"+pbDatepre);
			MyQuartzManager.removeJob(jobname3+"_1_"+pbDatepre);
			MyQuartzManager.removeJob(jobname4+"_1_"+pbDatepre);
			MyQuartzManager.removeJob(jobname1+"_2_"+pbDatepre);
			MyQuartzManager.removeJob(jobname2+"_2_"+pbDatepre);
			MyQuartzManager.removeJob(jobname3+"_2_"+pbDatepre);
			MyQuartzManager.removeJob(jobname4+"_2_"+pbDatepre);

			writelog.writeLog("开始添加:" + pbzxx.getShift() + "排班组定时任务");
			writelog.writeLog(pbzxx.getShift()+"排班人员userlist:"+personMap.get(pbzxx.getShift()));
			if (personMap.get(pbzxx.getShift()).size() == 0) {//如果班次没有人员则跳过该班次
				continue;
			}
			
			String card1 = pbzxx.getCard1();
			String card2 = pbzxx.getCard2();
			String card3 = pbzxx.getCard3();
			String card4 = pbzxx.getCard4();
			
			String card1date =pbDate;
			String card2date =pbDate;
			String card3date =pbDate;
			String card4date =pbDate;

			if (isValid(card1)) {

				card1 = card1date + " " + card1.substring(0, 5);
				Date date1 = DateUtil.StringToDate(card1, "yyyy-MM-dd HH:mm");
				Date startTime = DateUtil.dateCalculate(date1, -timeM);
				Date startTime2=DateUtil.dateCalculate(date1, timeL);
				Map<String, Object> map = new HashMap<String, Object>();
				String content="\n上班时间"+pbzxx.getCard1()+" 打卡扫描"+pbzxx.getScan1()+"-"+pbzxx.getEndT1();
				map.put("content", content1+content);
				map.put("userlist", personMap.get(pbzxx.getShift()));
				map.put("date",pbDate);
				map.put("card","card1");
				if (personMap.get(pbzxx.getShift()).size() > 0&&isOverCurrentDateTime(startTime)) {
					String jobdate=DateUtil.dateString(startTime, "yyyy-MM-dd");
					String jobtaskname1_1=jobname1+"_1_"+jobdate;
					writelog.writeLog("添加:" +jobtaskname1_1);
					MyQuartzManager.addJob(jobtaskname1_1, MyJob.class, startTime, map);
				}
				if (personMap.get(pbzxx.getShift()).size() > 0&&isOverCurrentDateTime(startTime2)) {
					String jobdate=DateUtil.dateString(startTime2, "yyyy-MM-dd");
					String jobtaskname1_2=jobname1+"_2_"+jobdate;
					writelog.writeLog("添加:" +jobtaskname1_2);
					MyQuartzManager.addJob(jobtaskname1_2, MyJob.class, startTime2, map);
				}
			}
			if (isValid(card2)) {
				if (DateUtil.compare_date(pbzxx.getCard2(), pbzxx.getCard1(), "HH:mm:ss") == -1) {//card2<card1,card2即跨下一天打卡
					card2date=DateUtil.datedayCalculateStr(DateUtil.StringToDate(card1date, "yyyy-MM-dd"), 1);
				}
				card2 = card2date + " " + card2.substring(0, 5);
				Date startTime = DateUtil.StringToDate(card2, "yyyy-MM-dd HH:mm");
				Date startTime2 = DateUtil.dateCalculate(startTime, timeL);
				Map<String, Object> map = new HashMap<String, Object>();
				String content="\n下班时间"+pbzxx.getCard2()+" 打卡扫描"+pbzxx.getScan2()+"-"+pbzxx.getEndT2();
				map.put("content", content2+content);
				map.put("userlist", personMap.get(pbzxx.getShift()));
				map.put("date",pbDate);
				map.put("card","card2");
				if (personMap.get(pbzxx.getShift()).size() > 0&&isOverCurrentDateTime(startTime)) {
					String jobdate=DateUtil.dateString(startTime, "yyyy-MM-dd");
					String jobtaskname2_1=jobname2+"_1_"+jobdate;
					writelog.writeLog("添加:" +jobtaskname2_1);
					MyQuartzManager.addJob(jobtaskname2_1, MyJob.class, startTime, map);
				}
				if (personMap.get(pbzxx.getShift()).size() > 0&&isOverCurrentDateTime(startTime2)) {
					String jobdate=DateUtil.dateString(startTime2, "yyyy-MM-dd");
					String jobtaskname2_2=jobname2+"_2_"+jobdate;
					writelog.writeLog("添加:" +jobtaskname2_2);
					MyQuartzManager.addJob(jobtaskname2_2, MyJob.class, startTime2, map);
				}
			}
			if (isValid(card3)) {
				if (DateUtil.compare_date(pbzxx.getCard3(), pbzxx.getCard2(), "HH:mm:ss") == -1) {
					card3date=DateUtil.datedayCalculateStr(DateUtil.StringToDate(card2date, "yyyy-MM-dd"), 1);
				}
				card3 = card3date + " " + card3.substring(0, 5);
				Date date3 = DateUtil.StringToDate(card3, "yyyy-MM-dd HH:mm");
				Date startTime = DateUtil.dateCalculate(date3, -timeM);
				Date startTime2 = DateUtil.dateCalculate(date3, timeL);
				Map<String, Object> map = new HashMap<String, Object>();
				String content="\n上班时间"+pbzxx.getCard3()+" 打卡扫描"+pbzxx.getScan3()+"-"+pbzxx.getEndT3();
				map.put("content", content1+content);
				map.put("userlist", personMap.get(pbzxx.getShift()));
				map.put("date",pbDate);
				map.put("card","card3");
				if (personMap.get(pbzxx.getShift()).size() > 0&&isOverCurrentDateTime(startTime)) {
					String jobdate=DateUtil.dateString(startTime, "yyyy-MM-dd");
					String jobtaskname3_1=jobname3+"_1_"+jobdate;
					writelog.writeLog("添加:" +jobtaskname3_1);
					MyQuartzManager.addJob(jobtaskname3_1, MyJob.class, startTime, map);
				}
				if (personMap.get(pbzxx.getShift()).size() > 0&&isOverCurrentDateTime(startTime)) {
					String jobdate=DateUtil.dateString(startTime2, "yyyy-MM-dd");
					String jobtaskname3_2=jobname3+"_2_"+jobdate;
					writelog.writeLog("添加:" +jobtaskname3_2);
					MyQuartzManager.addJob(jobtaskname3_2, MyJob.class, startTime2, map);
				}
			}
			if (isValid(card4)) {
				if (DateUtil.compare_date(pbzxx.getCard4(), pbzxx.getCard3(), "HH:mm:ss") == -1) {
					card4date=DateUtil.datedayCalculateStr(DateUtil.StringToDate(card3date, "yyyy-MM-dd"), 1);
				}
				card4 = card4date + " " + card4.substring(0, 5);
				Date startTime = DateUtil.StringToDate(card4, "yyyy-MM-dd HH:mm");
				Date startTime2 = DateUtil.dateCalculate(startTime, timeL);
				Map<String, Object> map = new HashMap<String, Object>();
				String content="\n下班时间"+pbzxx.getCard4()+" 打卡扫描"+pbzxx.getScan4()+"-"+pbzxx.getEndT4();
				map.put("content", content2+content);
				map.put("userlist", personMap.get(pbzxx.getShift()));
				map.put("date",pbDate);
				map.put("card","card4");
				if (personMap.get(pbzxx.getShift()).size() > 0&&isOverCurrentDateTime(startTime)) {
					String jobdate=DateUtil.dateString(startTime, "yyyy-MM-dd");
					String jobtaskname4_1=jobname4+"_1_"+jobdate;
					writelog.writeLog("添加:" +jobtaskname4_1);
					MyQuartzManager.addJob(jobtaskname4_1, MyJob.class, startTime, map);
				}
				if (personMap.get(pbzxx.getShift()).size() > 0&&isOverCurrentDateTime(startTime2)) {
					String jobdate=DateUtil.dateString(startTime2, "yyyy-MM-dd");
					String jobtaskname4_2=jobname4+"_2_"+jobdate;
					writelog.writeLog("添加:" +jobtaskname4_2);
					MyQuartzManager.addJob(jobtaskname4_2, MyJob.class, startTime2, map);
				}

			}

		}
	}

	/**
	 * 查询所有有效排班组
	 * 
	 * @return
	 */
	public static List<Pbzxx> getPBZlist() {
		List<Pbzxx> pbzlist = new ArrayList<Pbzxx>();
		RecordSet rs = new RecordSet();
		StringBuffer sb = new StringBuffer();
		sb.append(
				"select shift,title,scan1,card1,endT1,scan2,card2,endT2,scan3,card3,endT3,scan4,card4,endT4,Xhours,CardTimes from uf_pbzxx ");
		sb.append("where shift<>'R' and (IsDisabled='1' or IsDisabled is null)");

		boolean flag = rs.executeQuery(sb.toString());
		if (flag) {
			while (rs.next()) {
				Pbzxx pbz = new Pbzxx();
				pbz.setShift(rs.getString("shift"));
				pbz.setTitle(rs.getString("title"));
				pbz.setCard1(rs.getString("card1"));
				pbz.setCard2(rs.getString("card2"));
				pbz.setCard3(rs.getString("card3"));
				pbz.setCard4(rs.getString("card4"));
				pbz.setScan1(rs.getString("scan1"));
				pbz.setScan2(rs.getString("scan2"));
				pbz.setScan3(rs.getString("scan3"));
				pbz.setScan4(rs.getString("scan4"));
				pbz.setEndT1(rs.getString("endT1"));
				pbz.setEndT2(rs.getString("endT2"));
				pbz.setEndT3(rs.getString("endT3"));
				pbz.setEndT4(rs.getString("endT4"));
				pbz.setXhours(rs.getString("Xhours"));
				pbz.setCardTimes(rs.getString("CardTimes"));
				pbzlist.add(pbz);
			}
		}
		return pbzlist;
	}

	/**
	 * 根据排班组查询所有排班人员
	 * 
	 * @return
	 */
	public static Map<String, List<String>> getSendMsg(String dateDay) {
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		List<Pbzxx> pbzlist = getPBZlist();
		for (Pbzxx pbzxx : pbzlist) {
			List<String> ids = new ArrayList<String>();
			RecordSet rs = new RecordSet();
			StringBuffer sb = new StringBuffer();
			sb.append(
					"select h.id as id from hrmResource h inner join uf_rypbxx ry on h.outkey=ry.eid where CONVERT(date,ry.term,20)=? and ry.shift=?");
			boolean flag = rs.executeQuery(sb.toString(), dateDay, pbzxx.getShift());
			while (rs.next()) {
				ids.add(rs.getString("id"));
			}
			
			if("JiaBan".equalsIgnoreCase(pbzxx.getShift())) {
				String sql1="select applicant from uf_pbbdxx where convert(date,changeDate,20) =? "
						+ "and  convert(date,confirmdate,20)<=convert(date,datestart,20) and shift=?" ;
				RecordSet rs1 = new RecordSet();
				rs1.executeQuery(sql1,dateDay,pbzxx.getShift());
				while(rs1.next()) {
					ids.add(rs1.getString("applicant"));
				}
			}
			if ("PRJB".equalsIgnoreCase(pbzxx.getShift())) {
				String sql1="select applicant from uf_pbbdxx where convert(date,changeDate,20) =? "
						+ "and  convert(date,confirmdate,20)<=convert(date,datestart,20) and shift=?" ;
				RecordSet rs11 = new RecordSet();
				rs11.executeQuery(sql1,dateDay,pbzxx.getShift());
				while(rs11.next()) {
					ids.add(rs11.getString("applicant"));
				}
			}
			
			//将请假休息、补休整天、出差、出差加班的人排除通知
			String sql2="select * from uf_pbbdxx where changeDate=? and type in('0','1','4','7') and changepbday='1'";
			RecordSet rs2 = new RecordSet();
			rs2.executeQuery(sql2,dateDay);
			List<String> idsflow=new ArrayList<String>();
			while(rs2.next()) {
				idsflow.add(rs2.getString("applicant"));
			}
			ids.removeAll(idsflow);
			map.put(pbzxx.getShift(), ids);
			
		}

		return map;
	}

	/**
	 * 查询当天所有有排班的人员
	 */
	public void selectAllPb() {
		RecordSet rs = new RecordSet();
		StringBuffer sb = new StringBuffer();
		sb.append("select h.id as hid,ry.eid,ry.name, ry.term ,ry.xType,pbz.* from uf_rypbxx ry  inner join ");
		sb.append("uf_pbzxx pbz on ry.shift=pbz.shift and CONVERT(date,ry.term,20)=? ");
		sb.append(
				"and  pbz.shift<>'R' and (pbz.IsDisabled='1' or pbz.IsDisabled is null) inner join hrmResource h on ry.eid=h.outkey order by h.id");
		String currentDate = DateUtil.getCurrentDateTime("yyyy-MM-dd");
		boolean flag = rs.executeQuery(sb.toString(), currentDate);
		int i = 0;
		if (flag) {

			while (rs.next()) {
				i++;
				System.out.println(i + "|" + rs.getString("hid") + "|" + rs.getString("name"));
			}
		}

	}
	
	//大于当前时间则不添加任务，由于当前quartz版本较低，不能设在简单定时任务在什么时候执行，
	//只能设置开始时间和结束时间、重复次数、执行间隔，且定时时间<当前时间会立刻执行，所有计划任务startTime必须大于等于当前时间才符合逻辑
	private static boolean isOverCurrentDateTime(Date startTime) {
		Date currDate=new Date();
		return startTime.getTime()<currDate.getTime()?false:true;
		
	}
	
	///判断打卡card是否有效
	private static boolean isValid (String card) {
		if(!StringUtils.isEmpty(StringUtils.trim(card))&&!"NULL".equalsIgnoreCase(StringUtils.trim(card))) {
			return true;
		}
		return false;
	}
}
